package codetop.microsoft.T3;

import java.util.HashMap;
import java.util.Map;

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int left = 0, right = 0, maxLen = 0;
        boolean dup = false;
        Map<Character, Integer> window = new HashMap<>();
        while (right < s.length()) {
            char c = s.charAt(right++);
            int origin = window.getOrDefault(c, 0);
            if (origin > 0)
                dup = true;
            window.put(c, origin + 1);
            if (!dup)
                maxLen = Math.max(maxLen, right - left);
            while (dup) {
                char d = s.charAt(left++);
                int originVal = window.get(d);
                if (originVal == 2)
                    dup = false;
                window.put(d, originVal - 1);
            }
        }
        return maxLen;
    }
}
